package qeug.shsh.opplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.list.ListShowParameter;
import kd.bos.org.utils.OrgTreeUtils;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

public class PersonFilterBillPlugin extends AbstractBillPlugIn implements BeforeF7SelectListener {

    /*
     *  注册使用单据：付款申请单、资金计划和资金计划调整、差旅报销单、费用报销单
     *  使用字段：过滤字段包含：业务员、WBS、明细sap内部订单号、分摊sap内部订单号、同住人
     */
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        String DataEntityTypeNumber = this.getModel().getDataEntityType().getName();//单据标识编码
        if ("er_publicreimbursebill".equals(DataEntityTypeNumber)) {
            //付款申请单
            BasedataEdit qeug_salesman = this.getView().getControl("qeug_salesman");//付款申请单的业务员
            qeug_salesman.addBeforeF7SelectListener(this);
            BasedataEdit qeug_wbs = this.getView().getControl("qeug_wbs");//WBS
            qeug_wbs.addBeforeF7SelectListener(this);
            BasedataEdit qeug_orderenrtynum = this.getView().getControl("qeug_orderenrtynum");//明细sap内部订单号
            qeug_orderenrtynum.addBeforeF7SelectListener(this);
            BasedataEdit qeug_orderenrtynums = this.getView().getControl("qeug_orderenrtynums");//分摊sap内部订单号
            qeug_orderenrtynums.addBeforeF7SelectListener(this);
        } else if ("qeug_fundplan".equals(DataEntityTypeNumber) || "qeug_fundadjust".equals(DataEntityTypeNumber)) {
            //资金计划和资金计划调整
            BasedataEdit qeug_person = this.getView().getControl("qeug_person");//资金计划和资金计划调整的业务员
            qeug_person.addBeforeF7SelectListener(this);
            BasedataEdit qeug_fundsuse = this.getView().getControl("qeug_fundsuse");//资金计划和资金计划调整的款项用途
            qeug_fundsuse.addBeforeF7SelectListener(this);
        } else if ("er_tripreimbursebill".equals(DataEntityTypeNumber)) {
            //差旅报销单
            BasedataEdit control = this.getView().getControl("qeug_mulbasedatafield");//同住人
            control.addBeforeF7SelectListener(this);
            BasedataEdit qeug_wbs = this.getView().getControl("qeug_wbs");//WBS
            qeug_wbs.addBeforeF7SelectListener(this);
        } else if ("er_dailyreimbursebill".equals(DataEntityTypeNumber)) {
            //费用报销单
            BasedataEdit qeug_wbs = this.getView().getControl("qeug_wbs");//WBS
            qeug_wbs.addBeforeF7SelectListener(this);
            BasedataEdit qeug_orderenrtynum = this.getView().getControl("qeug_orderenrtynum");//明细sap内部订单号
            qeug_orderenrtynum.addBeforeF7SelectListener(this);
            BasedataEdit qeug_orderenrtynums = this.getView().getControl("qeug_orderenrtynums");//分摊sap内部订单号
            qeug_orderenrtynums.addBeforeF7SelectListener(this);
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        ListShowParameter formShowParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
        String DataEntityTypeNumber = this.getModel().getDataEntityType().getName();//单据标识编码
        IDataEntityProperty property = beforeF7SelectEvent.getProperty();
        String propertyName = property.getName();
        if ("er_publicreimbursebill".equals(DataEntityTypeNumber)) {
            if ("qeug_salesman".equals(propertyName)) {
                //付款申请单的业务员
                DynamicObject costDept = (DynamicObject) this.getModel().getValue("costdept");//费用承担部门
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1 = new QFilter("entryentity.dpt.number", QCP.ILIKE, costDept.getString("number") + "%");//费用部门部门与人员部门相同
                qFilter.add(f1);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
                formShowParameter.setCustomParam(OrgTreeUtils.PARAM_INIT_ROOT_ORG_ID, costDept.getString("id"));//设置左树根节点
            } else if ("qeug_wbs".equals(propertyName)) {
                //wbs
                DynamicObject costCompany = (DynamicObject) this.getModel().getValue("costcompany");//费用承担公司
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1 = new QFilter("qeug_company.number", QCP.ILIKE, costCompany.getString("number") + "%");//费用承担公司与辅助资料公司相同
                QFilter f2 = new QFilter("group.id", QCP.equals, 2162446542466719744L);//类别id
                qFilter.add(f1);
                qFilter.add(f2);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
            } else if ("qeug_orderenrtynum".equals(propertyName) || "qeug_orderenrtynums".equals(propertyName)) {
                //明细sap内部订单号、分摊sap内部订单号
                DynamicObject costCompany = (DynamicObject) this.getModel().getValue("costcompany");//费用承担公司
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1 = new QFilter("qeug_company.number", QCP.ILIKE, costCompany.getString("number") + "%");//费用承担公司与辅助资料公司相同
                QFilter f2 = new QFilter("group.id", QCP.equals, 2193589445591056384L);//类别id
                qFilter.add(f1);
                qFilter.add(f2);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
            }
        } else if ("qeug_fundplan".equals(DataEntityTypeNumber) || "qeug_fundadjust".equals(DataEntityTypeNumber)) {
            if ("qeug_person".equals(propertyName)) {
                //资金计划和资金计划调整的业务员
                DynamicObject costDept = (DynamicObject) this.getModel().getValue("qeug_deparment");//部门
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1 = new QFilter("entryentity.dpt.number", QCP.ILIKE, costDept.getString("number") + "%");//费用部门部门与人员部门相同
                qFilter.add(f1);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
                formShowParameter.setCustomParam(OrgTreeUtils.PARAM_INIT_ROOT_ORG_ID, costDept.getString("id"));//设置左树根节点
            } else if ("qeug_fundsuse".equals(propertyName)) {
                //资金计划和资金计划调整的款项用途
                DynamicObject billType = (DynamicObject) this.getModel().getValue("qeug_billtype");//单据类型
                if (billType == null) {
                    this.getView().showErrorNotification("请先填写单据类型");
                    beforeF7SelectEvent.setCancel(true);//阻止弹出窗体
                    return;
                }
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1;
                if ("qeug_fundplan_01".equals(billType.getString("number")) || "qeug_fundplan_03".equals(billType.getString("number")) || "qeug_fundadjust_01".equals(billType.getString("number"))) {
                    //资金计划月支出和周支出和资金计划调整月支出
                    f1 = new QFilter("fullname", QCP.ILIKE, "%支出%");//存在支出

                } else {
                    f1 = new QFilter("fullname", QCP.ILIKE, "%收入%");//存在收入
                }
                QFilter f2 = new QFilter("level", QCP.equals, 3);
                qFilter.add(f1);
                qFilter.add(f2);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
                formShowParameter.getTreeFilterParameter().getQFilters().add(f1);//设置左树根节点过滤
            }
        } else if ("er_tripreimbursebill".equals(DataEntityTypeNumber)) {
            if ("qeug_mulbasedatafield".equals(propertyName)) {
                //差旅报销单的同住人
                DynamicObject costDept = (DynamicObject) this.getModel().getValue("costdept");//费用承担部门
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1 = new QFilter("entryentity.dpt.number", QCP.ILIKE, costDept.getString("number") + "%");//费用部门部门与人员部门相同
                qFilter.add(f1);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
                formShowParameter.setCustomParam(OrgTreeUtils.PARAM_INIT_ROOT_ORG_ID, costDept.getString("id"));//设置左树根节点
            } else if ("qeug_wbs".equals(propertyName)) {
                //wbs
                DynamicObject costCompany = (DynamicObject) this.getModel().getValue("costcompany");//费用承担公司
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1 = new QFilter("qeug_company.number", QCP.ILIKE, costCompany.getString("number") + "%");//费用承担公司与辅助资料公司相同
                QFilter f2 = new QFilter("group.id", QCP.equals, 2162446542466719744L);//类别id
                qFilter.add(f1);
                qFilter.add(f2);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
            }
        } else if ("er_dailyreimbursebill".equals(DataEntityTypeNumber)) {
            //费用报销单
            if ("qeug_wbs".equals(propertyName)) {
                //wbs
                DynamicObject costCompany = (DynamicObject) this.getModel().getValue("costcompany");//费用承担公司
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1 = new QFilter("qeug_company.number", QCP.ILIKE, costCompany.getString("number") + "%");//费用承担公司与辅助资料公司相同
                QFilter f2 = new QFilter("group.id", QCP.equals, 2162446542466719744L);//类别id
                qFilter.add(f1);
                qFilter.add(f2);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
            } else if ("qeug_orderenrtynum".equals(propertyName) || "qeug_orderenrtynums".equals(propertyName)) {
                //明细sap内部订单号、分摊sap内部订单号
                DynamicObject costCompany = (DynamicObject) this.getModel().getValue("costcompany");//费用承担公司
                List<QFilter> qFilter = new ArrayList<>();
                QFilter f1 = new QFilter("qeug_company.number", QCP.ILIKE, costCompany.getString("number") + "%");//费用承担公司与辅助资料公司相同
                QFilter f2 = new QFilter("group.id", QCP.equals, 2193589445591056384L);//类别id
                qFilter.add(f1);
                qFilter.add(f2);
                formShowParameter.getListFilterParameter().setQFilters(qFilter);
                formShowParameter.setCustomParam("isIncludeAllSub", "true");//设置包含下级为true
            }
        }
    }
}
